package com.ylbh.modules.goods.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.goods.dto.GoodsDTO;
import com.ylbh.common.pojo.goods.dto.GoodsListDTO;
import com.ylbh.common.pojo.goods.vo.GoodsExcelVO;
import com.ylbh.common.pojo.goods.vo.GoodsListVO;
import com.ylbh.common.pojo.goods.vo.ManufactorVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.HttpClientHelperUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.goods.mapper.SupplierGoodsMapper;
import com.ylbh.modules.goods.service.GoodsSpecService;
import com.ylbh.modules.goods.service.SupplierGoodsService;
import com.ylbh.modules.sys.dao.SysRoleDao;
import com.ylbh.modules.sys.dao.SysUserDao;
import com.ylbh.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;


/**
 * @author mgh
 */
@Service
public class SupplierGoodsServiceImpl extends ServiceImpl<SupplierGoodsMapper, GoodsDTO> implements SupplierGoodsService {

    private final Logger log = LoggerFactory.getLogger(ShoppingGoodsServiceImpl.class);

    @Autowired
    private SupplierGoodsMapper supplierGoodsMapper;
    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private SysUserDao sysUserDao;

    @Value("${url.shoppingMainImage}")
    private String shoppingMainImage;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Value("${url.addEsGoods}")
    private String addEsGoods;

    @Value("${url.removeEsGoods}")
    private String removeEsGoods;

    /**
     * 商品信息列表
     * @param goodsListDTO
     * @return
     */
    @Override
    public PageUtils queryGoodsList(GoodsListDTO goodsListDTO) {
        Integer role = sysRoleDao.selectRole(goodsListDTO.getGoodsManagerId().longValue());
        // 角色类型是6表示是供应商
        if (null == role || role != 6) {
            goodsListDTO.setGoodsManagerId(null);
        }
        Page<Object> page = new Page<>(goodsListDTO.getPage(), goodsListDTO.getLimit());
        List<GoodsListVO> list = supplierGoodsMapper.selectGoodsList(goodsListDTO, page);

        for (GoodsListVO vo : list) {
            // 如果供应商商品已经上架了
            if (vo.getGoodsStatus() == 0 && role == 6) {
                vo.setUpdateStatus(1);
            } else {
                vo.setUpdateStatus(0);
            }
            String sku = vo.getGoodsInventoryDetail();
            if(StringUtils.isNotBlank(sku)){
                vo.setGoodsInventoryDetail(goodsSpecService.sortOutSku(sku));
            }
            if (!StringUtils.isEmpty(vo.getName()) && !StringUtils.isEmpty(vo.getPath())) {
                vo.setImg(shoppingMainImage + vo.getPath() + "/" + vo.getName());
            }

            if (null != vo.getId()) {
                vo.setUrl("http://www.yilianbaihui.cn/item_show.html?goodsId=" + vo.getId());
            }

        }
        Page<GoodsListVO> page1 = new Page<>();
        page1.setRecords(list).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    /**
     * 运营商厂家列表
     * @return
     */
    @Override
    public List<ManufactorVO> queryManufactorList() {
        return supplierGoodsMapper.selectManufactorList();
    }

    /**
     * 查询出当前用户的角色类型
     * @param userId
     * @return
     */
    @Override
    public Integer queryRoleType(Long userId) {
        return sysRoleDao.selectRole(userId);
    }

    /**
     * 修改供应商商品的上下架状态
     * @param id
     * @param type
     * @param userId
     */
    @Override
    public void modifyGoodsStatusById(String id, Integer type, Long userId) {
        Integer role = sysRoleDao.selectRole(userId);
        // 如果角色类型是6，说明是供应商，所以下架状态是-1，反之是管理员下架为-2
        if (role == 6) {
            supplierGoodsMapper.updateGoodsStatusById(id, type, -1);
        } else {
            supplierGoodsMapper.updateGoodsStatusById(id, type, -2);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id + "");
        if (type == 0) {
            // 上架
            String msg = HttpClientHelperUtils.sendHttpPostEs(addEsGoods, map);
            log.info("=========================上架商品添加到es服务器返回信息:" + msg);
        } else {
            // 下架

            String httpPost = HttpClientHelperUtils.sendHttpPostEs(removeEsGoods, map);
            log.info("=============================下架商品同时删除es商品信息：" + httpPost);
        }
    }

    /**
     * 供应商用户列表
     * @return
     */
    @Override
    public List<SysUserEntity> queryAllRole() {
        return sysUserDao.selectAllRole();
    }
}
